﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.Workflow;

namespace SPDWorkflowStringActions
{
    public class SubStringStart : Activity
    {
        public static DependencyProperty InParam1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam1", typeof(string), typeof(SubStringStart));

        public static DependencyProperty InParam2Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "InParam2", typeof(string), typeof(SubStringStart));

        public static DependencyProperty OutResult1Property
            = System.Workflow.ComponentModel.DependencyProperty.Register(
            "OutResult1", typeof(string), typeof(SubStringStart));

        public SubStringStart()
        {
            this.Name = "SubStringStart";
        }

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam1
        {
            get
            {
                return ((string)(base.GetValue(SubStringStart.InParam1Property)));
            }
            set
            {
                base.SetValue(SubStringStart.InParam1Property, value);
            }
        } // public string InParam1

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string InParam2
        {
            get
            {
                return ((string)(base.GetValue(SubStringStart.InParam2Property)));
            }
            set
            {
                base.SetValue(SubStringStart.InParam2Property, value);
            }
        } // public string InParam2

        [Category("Dispatcher"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutResult1
        {
            get
            {
                return ((string)(base.GetValue(SubStringStart.OutResult1Property)));
            }
            set
            {
                base.SetValue(SubStringStart.OutResult1Property, value);
            }
        } // public string OutResult1


        protected override ActivityExecutionStatus
          Execute(ActivityExecutionContext executionContext)
        {

            this.OutResult1 = this.InParam1.Substring(Convert.ToInt32(InParam2));

            return ActivityExecutionStatus.Closed;
        }
        protected override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
        {
            try
            {
                ((ISharePointService)executionContext.GetService(typeof(ISharePointService))).LogToHistoryList(
                    base.WorkflowInstanceId, SPWorkflowHistoryEventType.WorkflowError, 0,
                    TimeSpan.MinValue, string.Empty,
                    string.Format("Error occured when performing \"" + this.InParam1 + "\".SubString(" + this.InParam2 + "): {0}", exception.Message), string.Empty);

            }
            catch (Exception ex)
            { }
            return base.HandleFault(executionContext, exception);
        }
    }
}
